using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace IWS.Lib.SingleSignOnServer
{
	public class Utility
	{
		public static string GetRandomString(int length)
		{
			//returns random string of length specified with characters including: 0-9, a-z, A-Z
			char[] ca = new char[length];
			byte[] random = new Byte[length];
			//RNGCryptoServiceProvider is an implementation of a random number generator.

			RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
			rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.

			for (int i = 0; i < length; i++)
			{
				bool found = false;
				int rand = (int)random[i];
				while (!found)
				{
					if (((rand >= 48) && (rand <= 57)) || ((rand >= 65) && (rand <= 90)) || ((rand >= 97) && (rand <= 122)))
					{
						found = true;
					}
					else
					{
						//get a new random int. 
						byte[] single = new byte[1];
						rng.GetBytes(single);
						rand = single[0];
					}
				}
				char ci = (char)rand;
				ca[i] = ci;
			}
			string s = new string(ca);
			return s;
		}
		public static string GetRandomUnicodeString(int length)
		{
			//RNGCryptoServiceProvider is an implementation of a random number generator.
			byte[] random = new Byte[length * 2]; //two bytes per character in unicode. 

			RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
			rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.

			return System.Text.UnicodeEncoding.Unicode.GetString(random);
		}
	}
}
